package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements IBannerService {

	@Autowired
	private BannerRepository bannerRepository;
	@Autowired
	private MongoTemplate template;
	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private ITravelService travelService;

	@Override
	public void save(Banner banner) {
		bannerRepository.save(banner);
	}

	@Override
	public void update(Banner banner) {
		bannerRepository.save(banner);
	}

	@Override
	public void delete(String id) {
		bannerRepository.deleteById(id);
	}

	@Override
	public Banner get(String id) {
		return bannerRepository.findById(id).orElse(null);
	}

	@Override
	public List<Banner> list() {
		return bannerRepository.findAll();
	}

	@Override
	public Page query(QueryObject qo) {
		Query query = new Query();
		return PageUtils.page(template, query, Banner.class, qo,null);
	}

	@Override
	public Map<String, Object> getAllType() {
		Map<String,Object> map = new HashMap<>();
		List<Strategy> sts = strategyService.list();
		List<Travel> ts = travelService.list();
		map.put("sts",sts);
		map.put("ts",ts);

		return map;
	}

	@Override
	public List<Banner> getTravels() {
		Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"sequence");
		return bannerRepository.findByType(Banner.TYPE_TRAVEL,pageable);
	}

	@Override
	public Banner getStrategy() {
		return bannerRepository.findBySequenceAndType(1,Banner.TYPE_STRATEGY);
	}

}
